<?php

class Profile extends Controller {

	function Profile(){
		parent::Controller();	
	}
	function _setConstant(){
		$data['js1'] = 'jquery-1.4.2.js';
		$data['js2'] = 'jquery.form.js';
		$data['js3'] = "jquery.validate.pack.js";
		$data['aboutLink'] = base_url().'main/about';
		$data['contactLink'] = base_url().'main/contact';
		$data['helpLink'] = base_url().'main/help';
		return $data;
	}
	function index(){
		if(!$this->session->userdata('isLogin')){
				redirect('/home/');
		}else{
			redirect('/profile/info');
		}
	}
	
	function info($username=NULL){
		$this->load->model('User');
		$this->load->model('UsersDetail');
		$this->load->model('UserMahasiswa');
		$this->load->model('Friend');
		$data = $this->_setConstant();
		
		$data['isMyFriend'] = FALSE;
		$data['isWait']=FALSE;
		if(!$username){
			if(!$this->session->userdata('isLogin')){
				redirect('/home/');
			}else{
				$username = $this->User->getUsername($this->session->userdata('userId'));
				if(!$username){
					redirect('/dashboard/');
				}else{
					$data['isMyProfile'] = TRUE;
					$data['nama'] = json_decode($this->UserMahasiswa->getNama($this->session->userdata('userId')))->nama;
					$data['nim'] = json_decode($this->UserMahasiswa->getNim($this->session->userdata('userId')))->nim;
					$detail = json_decode($this->UsersDetail->getDetail($this->session->userdata('userId')));
					$data ['avatar'] = $detail->avatar;
					
					if($data['avatar']==''){
						$data['avatar'] = 'default_avatar.jpg';
					}
					$data['nama_prof'] = $data['nama'];
					$data ['tglLahir'] = $detail->tglLahir;
					$data ['gender'] = $detail->gender;
					$data ['location'] = $detail->location;
					$data ['about_me'] = $detail->about_me;
					$data ['reg_date'] = $detail->reg_date;
					$data ['lastlogin_date'] = $detail->lastlogin_date;
					$data ['username'] = $this->User->getUsername($this->session->userdata('userId'));
					$data ['pageTitle'] = 'Profile '.$data['nama'];
					$this->load->view('profileInfo',$data);
				}
			}
		}else{
			if(!$this->User->isExistActive($username)){
				if(!$this->session->userdata('isLogin')){
					redirect('/home/');
				}else{
					redirect('/dashboard/');
				}
			}else{
				if(!$this->session->userdata('isLogin')){
					$data['isMyProfile'] = FALSE;
					$data['nama'] = 'i-see';
					$data['nama_prof'] = json_decode($this->UserMahasiswa->getNama($this->UsersDetail->getUserIdFromUsername($username)))->nama;
					$data['nim'] = json_decode($this->UserMahasiswa->getNim($this->UsersDetail->getUserIdFromUsername($username)))->nim;
					$detail = json_decode($this->UsersDetail->getDetail($this->UsersDetail->getUserIdFromUsername($username)));
					$data ['avatar'] = $detail->avatar;
					
					
					if($data['avatar']==''){
						$data['avatar'] = 'default_avatar.jpg';
					}
					$data ['tglLahir'] = $detail->tglLahir;
					$data ['gender'] = $detail->gender;
					$data ['location'] = $detail->location;
					$data ['about_me'] = $detail->about_me;
					$data ['reg_date'] = $detail->reg_date;
					$data ['lastlogin_date'] = $detail->lastlogin_date;
					$data ['username'] = $username;
					$data ['pageTitle'] = 'Profile '.$data['nama_prof'];
					$this->load->view('profileInfo',$data);
				}else{
					$MYusername = $this->User->getUsername($this->session->userdata('userId'));
					if($username==$MYusername){
						$data['isMyProfile'] = TRUE;
						$data['nama'] = json_decode($this->UserMahasiswa->getNama($this->session->userdata('userId')))->nama;
						$data['nim'] = json_decode($this->UserMahasiswa->getNim($this->session->userdata('userId')))->nim;
						$detail = json_decode($this->UsersDetail->getDetail($this->session->userdata('userId')));
						$data ['avatar'] = $detail->avatar;
						
						if($data['avatar']==''){
							$data['avatar'] = 'default_avatar.jpg';
						}
						$data['nama_prof'] = $data['nama'];
						$data ['tglLahir'] = $detail->tglLahir;
						$data ['gender'] = $detail->gender;
						$data ['location'] = $detail->location;
						$data ['about_me'] = $detail->about_me;
						$data ['lastlogin_date'] = $detail->lastlogin_date;
						$data ['reg_date'] = $detail->reg_date;
						$data ['username'] = $username;
						$data ['pageTitle'] = 'Profile '.$data['nama'];
						$this->load->view('profileInfo',$data);
					//profilpage
					}else{
						$data['isMyProfile'] = FALSE;
						$data['nama'] = json_decode($this->UserMahasiswa->getNama($this->session->userdata('userId')))->nama;
						$data['nama_prof'] = json_decode($this->UserMahasiswa->getNama($this->UsersDetail->getUserIdFromUsername($username)))->nama;
						$data['nim'] = json_decode($this->UserMahasiswa->getNim($this->UsersDetail->getUserIdFromUsername($username)))->nim;
						$detail = json_decode($this->UsersDetail->getDetail($this->UsersDetail->getUserIdFromUsername($username)));
						$data ['avatar'] = $detail->avatar;
						
						$data['isMyFriend'] = $this->Friend->isMyFriend($this->session->userdata('userId'),$this->UsersDetail->getUserIdFromUsername($username));
						$data['isWait'] = $this->Friend->isWait($this->session->userdata('userId'),$this->UsersDetail->getUserIdFromUsername($username));
						$data['id_prof'] = $this->UsersDetail->getUserIdFromUsername($username);
						if($data['avatar']==''){
							$data['avatar'] = 'default_avatar.jpg';
						}
						$data ['tglLahir'] = $detail->tglLahir;
						$data ['gender'] = $detail->gender;
						$data ['location'] = $detail->location;
						$data ['about_me'] = $detail->about_me;
						$data ['reg_date'] = $detail->reg_date;
						$data ['lastlogin_date'] = $detail->lastlogin_date;
						$data ['username'] = $username;
						$data ['pageTitle'] = 'Profile '.$data['nama_prof'];
						$this->load->view('profileInfo',$data);
					}
					//profilpage
				}
			}
		}
	}
	
	function contact($username=NULL){
		$this->load->model('User');
		$this->load->model('UsersDetail');
		$this->load->model('UserMahasiswa');
		$data = $this->_setConstant();
		$data ['username'] = $username;
		if(!$username){
			if(!$this->session->userdata('isLogin')){
				redirect('/home/');
			}else{
				$username = $this->User->getUsername($this->session->userdata('userId'));
				if(!$username){
					redirect('/dashboard/');
				}else{
					$username = $this->User->getUsername($this->session->userdata('userId'));
					redirect("/profile/contact/$username");
				}
			}
		}else{
			if(!$this->User->isExistActive($username)){
				if(!$this->session->userdata('isLogin')){
					redirect('/home/');
				}else{
					redirect('/dashboard/');
				}
			}else{
				$detail = json_decode($this->UsersDetail->getDetail($this->UsersDetail->getUserIdFromUsername($username)));
				if(!$this->session->userdata('isLogin')){
					$data['isMyProfile'] = FALSE;
					$data['nama'] = 'i-see';
					$data['nama_prof'] = json_decode($this->UserMahasiswa->getNama($this->UsersDetail->getUserIdFromUsername($username)))->nama;
					$data['email'] = json_decode($this->UserMahasiswa->getNama($this->UsersDetail->getUserIdFromUsername($username)))->email;
				}else{
					$MYusername = $this->User->getUsername($this->session->userdata('userId'));
					$data['nama'] = json_decode($this->UserMahasiswa->getNama($this->session->userdata('userId')))->nama;
					$data['email'] = json_decode($this->UserMahasiswa->getNama($this->UsersDetail->getUserIdFromUsername($username)))->email;
					$data['isMyProfile'] = FALSE;
					if($username==$MYusername){
						$data['isMyProfile'] = TRUE;
					}
				}
				$data ['avatar'] = $detail->avatar;
				if($data['avatar']==''){
					$data['avatar'] = 'default_avatar.jpg';
				}
				$data['nama_prof'] = json_decode($this->UserMahasiswa->getNama($this->UsersDetail->getUserIdFromUsername($username)))->nama;
				$detail = json_decode($this->UsersDetail->getDetail($this->UsersDetail->getUserIdFromUsername($username)));
				$data['website'] = $detail->website;
				$data['work_phone'] = $detail->work_phone;
				$data['personal_phone'] = $detail->personal_phone;
				$data['im_skype'] = $detail->im_skype;
				$data['im_gtalk'] = $detail->im_gtalk;
				$data['im_msn'] = $detail->im_msn;
				$data ['im_msn']= $detail->im_msn;
	  			$data ['im_yahoo']= $detail->im_yahoo;
	  			$data ['im_aim']= $detail->im_aim;
	  			$data ['prof_facebook']= $detail->prof_facebook;
	  			$data ['prof_twitter']= $detail->prof_twitter;
	  			$data ['prof_flickr']= $detail->prof_flickr;
	  			$data ['prof_friendfeed']= $detail->prof_friendfeed;
	  			$data ['prof_myspace']= $detail->prof_myspace;
	  			$data ['prof_youtube']= $detail->prof_youtube;
				$data ['pageTitle'] = 'Contact '.$data['nama_prof'];
				$this->load->view('profileContact',$data);
			}
		}
	}
	
	function echos($username=NULL){
		$this->load->model('User');
		$this->load->model('UsersDetail');
		$this->load->model('UserMahasiswa');
		$this->load->model('Posts');
		$data = $this->_setConstant();
		$data ['username'] = $username;
		if(!$username){
			if(!$this->session->userdata('isLogin')){
				redirect('/home/');
			}else{
				$username = $this->User->getUsername($this->session->userdata('userId'));
				if(!$username){
					redirect('/dashboard/');
				}else{
					$username = $this->User->getUsername($this->session->userdata('userId'));
					redirect("/profile/echo/$username");
				}
			}
		}else{
			if(!$this->User->isExistActive($username)){
				if(!$this->session->userdata('isLogin')){
					redirect('/home/');
				}else{
					redirect('/dashboard/');
				}
			}else{
				$detail = json_decode($this->UsersDetail->getDetail($this->UsersDetail->getUserIdFromUsername($username)));
				if(!$this->session->userdata('isLogin')){
					$data['isMyProfile'] = FALSE;
					$data['nama'] = 'i-see';
					$data['nama_prof'] = json_decode($this->UserMahasiswa->getNama($this->UsersDetail->getUserIdFromUsername($username)))->nama;
				}else{
					$MYusername = $this->User->getUsername($this->session->userdata('userId'));
					$data['nama'] = json_decode($this->UserMahasiswa->getNama($this->session->userdata('userId')))->nama;
					$data['isMyProfile'] = FALSE;
					if($username==$MYusername){
						$data['isMyProfile'] = TRUE;
					}
				}
				$data ['avatar'] = $detail->avatar;
				if($data['avatar']==''){
					$data['avatar'] = 'default_avatar.jpg';
				}
				$data['nama_prof'] = json_decode($this->UserMahasiswa->getNama($this->UsersDetail->getUserIdFromUsername($username)))->nama;
				$detail = json_decode($this->UsersDetail->getDetail($this->UsersDetail->getUserIdFromUsername($username)));
				$data ['pageTitle'] = 'Echos '.$data['nama_prof'];
				
				$data['myEcho'] = $this->Posts->getMyPost($this->UsersDetail->getUserIdFromUsername($username));
				if($myEcho = $data['myEcho']){
					$myEchoMsg ="";
					for($i=0;$i<sizeOf($myEcho['message']);$i++){
						$myEchoMsg = $myEchoMsg.$this->_formatEcho($myEcho['user_id'][$i],$myEcho['message'][$i],$myEcho['date'][$i],$myEcho['comments'][$i],$myEcho['post_id'][$i]);	
					}
					$data['myEchoMsg'] = $myEchoMsg;
				}
				$echos = $this->Posts->getMyPost($this->UsersDetail->getUserIdFromUsername($username),0,9999999);
				$data['num_messages'] =  sizeOf($echos['message']);
				$data['page'] = "user";
				$data['username'] = $username;
				$this->load->view('profileEchos',$data);
			}
		}
	}
	
	function _formatEcho($user_id,$message,$date,$commentNum=NULL,$post_id=NULL){
		
		$this->load->model('UsersDetail');
		$this->load->model('UserMahasiswa');
		$this->load->model('User');
		$this->load->model('Posts');
		$this->load->model('Attachment');
		$thisPost = $this->Posts->getPost($post_id);
		
		
		$attachedHTML='<ul id="listAttach">';
		if($thisPost['attached']==1){
			$postAttach = $this->Attachment->getAttachment($post_id);
			for($i=0;$i<sizeOf($postAttach['type']);$i++){
				if($postAttach['type'][$i]=='image'){
					$attachedHTML = $attachedHTML.'<li>
					<a href="'.base_url().'sknimg/i-see-attr/imageAttachment/'.$postAttach['data'][$i].'" class="lightbox-enabled" onclick="$(this).lightbox({start:true,events:false}); return false;" title="'.$postAttach['data'][$i].'">
						<img class="avatarecho" style="float:right;" src="'.base_url().'sknimg/i-see-attr/imageAttachmentThumb/'.$postAttach['data'][$i].'" width="50px" height="50px"  /></a>
					<br><strong style="font-size:11px;font-style:italic;">
					<img src="'.base_url().'sknimg/img/download.png" width="15" height="15"></img>
					<a href="'.base_u